package com.smartfast4j.springboot.redis.demo.message;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.concurrent.CountDownLatch;

/**
 * 创建一个消息接收者
 * Receiver类，它是一个普通的类，需要注入到SpringBoot中。
 * Created by gacl on 2017/11/21.
 */
public class Receiver {

    private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);

    /**
     * CountDownLatch是一个同步工具类，它允许一个或多个线程一直等待，直到其他线程的操作执行完后再执行。
     */
    private CountDownLatch latch;

    @Autowired
    public Receiver(CountDownLatch latch) {
        this.latch = latch;
    }

    /**
     * 接收消息
     * @param message
     */
    public void receiveMessage(String message) {
        LOGGER.info("Receiver→接收到的消息：message：" + message);
        latch.countDown();
    }
}
